package com.xiaolin.sports.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaolin.sports.dao.ParentInfoMapper;
import com.xiaolin.sports.model.ParentInfo;
import com.xiaolin.sports.page.Page;
import com.xiaolin.sports.service.IParentInfoService;
import com.xiaolin.sports.view.ParentInfoView;
import com.xiaolin.sports.wechat.model.WechatUserInfo;

@Service("parentInfoService")
public class ParentInfoServiceImpl implements IParentInfoService {
	
	@Autowired
	private ParentInfoMapper parentInfoMapper;
	
	@Override
	public ParentInfo selectByOpenid(String openid) {
		return parentInfoMapper.selectByOpenid(openid);
	}

	@Override
	public int selectCountByOpenid(String openid) {
		return parentInfoMapper.selectCountByOpenid(openid);
	}

	@Override
	public List<ParentInfo> selectListByPage(Page<ParentInfo> page) {
		return parentInfoMapper.selectListByPage(page);
	}

	@Override
	public ParentInfo selectById(Integer custId) {
		return parentInfoMapper.selectByPrimaryKey(custId);
	}
	
	@Override
	public List<ParentInfoView> selectBuyTotalByPage(Page<ParentInfoView> page) {
		return parentInfoMapper.selectBuyTotalByPage(page);
	}

	@Override
	public ParentInfo insertReturn(WechatUserInfo userInfo) {
		ParentInfo parentInfo = new ParentInfo();
		parentInfo.setHeadImg(userInfo.getHeadimgurl());
		parentInfo.setCity(userInfo.getCity());
		parentInfo.setProvince(userInfo.getProvince());
		parentInfo.setNickName(userInfo.getNickname());
		parentInfo.setOpenid(userInfo.getOpenid());
		parentInfoMapper.insertSelective(parentInfo);
		return parentInfo;
	}

	@Override
	public int addAndUpdate(ParentInfo info) {
		if(info.getId() == null) {
			return parentInfoMapper.insertSelective(info);
		}
		info.setUpdateTime(new Date());
		return parentInfoMapper.updateByPrimaryKeySelective(info);
	}

}
